#pragma once


#include "db/mysql/MysqlDB.h"
#include <iostream>
#include <common/DateTime.h>


inline void mysql_test()
{
    DBConfig config;
    config.Host = "localhost";
    config.Port = 3306;
    config.User = "root";
    config.Password = "newpassword";
    config.Database = "new_account";
    config.Charset = "gbk";
    MysqlDB db;
    db.open(config);

    auto create_sql = "CREATE TABLE `mixtest` ("
        "`IntField` int(9) NOT NULL DEFAULT 0,"
        "`FloatField` double NOT NULL DEFAULT 0,"
        "`StringField` varchar(64) NOT NULL DEFAULT '',"
        "`DateField` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',"
        "`TimeStampField` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,"
        " PRIMARY KEY (`IntField`)"
        ") ENGINE = InnoDB DEFAULT CHARSET = gbk; ";

    {
        db.exec("drop table mixtest");
        assert(db.exec(create_sql) >= 0);
    }
   
    {
        assert(1 == db.exec("insert into mixtest(IntField,FloatField,StringField,DateField,TimeStampField) "
            "values(?,?,?,now(),?)", 1, 3.14, "or 1=1",  DateTime::currentDateTime().toString()));
        char str[] = "string";
        assert(1 == db.exec("insert into mixtest(IntField,FloatField,StringField,DateField,TimeStampField) "
            "values(?,?,?,?,?)", 2, 2.22, str, DateTime::currentDateTime().toString(), DateTime::currentDateTime().toString()));

    }
   
    {
        DBRows data;
        assert(db.query("select IntField,FloatField,StringField,DateField,TimeStampField from mixtest", data));
        assert(data.size() == 2);
        for (auto it = data.begin(); it != data.end(); ++it) {
            int IntField;
            float FloatField;
            std::string StringField;
            std::string DateField;
            char TimeStampField[32] = {};
            assert(it->scan(IntField, FloatField, StringField, DateField, TimeStampField));
            printf("%d %f %s %s %s\n", IntField, FloatField, StringField.c_str(), DateField.c_str(), TimeStampField);
        }
    }
    
    {
        assert(1 == db.exec("update mixtest set FloatField = ? where IntField = ?", 9.99, 1));
        DBRows data;
        assert(db.query("select FloatField from mixtest where IntField = 1", data));
        assert(data.size() == 1);
        assert(data[0]["FloatField"].to_double() == 9.99);
    }
    
    {
        assert(1 == db.exec("delete from mixtest where IntField = 1"));
    }
   
    std::cout << "mysql test end " << 1 << std::endl;
}